package lolskins.utils;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class LOLXMLParser {

    private Hashtable<String, Object> data;

    public LOLXMLParser(String fileName) {
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(fileName));
            doc.getDocumentElement().normalize();
            System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());
            parse(doc);
        } catch (SAXException ex) {
            Logger.getLogger(LOLXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LOLXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(LOLXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void parse(Document doc) {

        NodeList listOfKVPairs = doc.getElementsByTagName("KVPair");
        int totalKVPairs = listOfKVPairs.getLength();
        System.out.println("Total KVPairs: " + totalKVPairs);

        for (int i = 0; i < totalKVPairs; i++) {

            Node KVPairNode = listOfKVPairs.item(i);
            if (KVPairNode.getNodeType() == Node.ELEMENT_NODE) {
                Element KVPairElement = (Element) KVPairNode;

                NodeList keyNodeList = KVPairElement.getElementsByTagName("Key");
                Element keyElement = (Element) keyNodeList.item(0);
                System.out.println("Key: " + ((Node) keyElement.getChildNodes().item(0)).getNodeValue().trim());

                NodeList valueNodeList = KVPairElement.getElementsByTagName("Value");
                Element valueElement = (Element) valueNodeList.item(0);

                NodeList textNodeList = valueElement.getElementsByTagName("Text");
                if (textNodeList.getLength() != 0) {
                    Element textElement = (Element) textNodeList.item(0);
                    System.out.println("Value Text: " + ((Node) textElement.getChildNodes().item(0)).getNodeValue().trim());
                } else {
//                    NodeList tableNodeList = valueElement.getElementsByTagName("SubTable");
//                    this.parse(tableNodeList);
//                    System.out.println("Its a table go figure something out");
                }
            }
        }
    }

    public static void main(String[] args) {
        LOLXMLParser parser = new LOLXMLParser("src/lolskins/xmlAdminSettings.xml");
    }

    public Hashtable<String, Object> getData() {
        return data;
    }
}
